package com.henry.newscafe;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


class NewsSource extends Observable {
    List<String> articles;
    NewsSource() {
        articles = new ArrayList<>();
    }

    public String getNewestArticle() {
        return articles.get(articles.size() - 1);
    }

    public void addArticle(String title) {
        articles.add(title);
        setChanged();
        notifyObservers(new String("New Article! "));
    }

}

class MyReader implements Observer {
    @Override
    public void update(Observable o, Object arg) { // Read News
        System.out.println(arg.toString() + ((NewsSource)o).getNewestArticle());
    }
}

public class UtilObserverDemo {
    public static void main(String[] args) {
        Observer reader = new MyReader();
        NewsSource feed = new NewsSource();
        feed.addObserver(reader);
        feed.addArticle("Good Weather!");
    }
}
